package engine.ui.chat
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.display.newAttack.AttackLogic;
	import engine.event.SceneEvent;
	import engine.flag.GameDefine;
	import engine.logic.ChatFilterLogic;
	import engine.logic.MenuListManager;
	import engine.logic.tips.ItemMaxTips;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.newUi.MessageUI;
	import engine.newUi.WhisperLogic;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.FaceIconViewUI;
	import engine.ui.UIManage;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.list.NxList;
	import newx.com.control.list.NxListEvent;
	import newx.com.control.scroll.text.ChatText;
	import newx.com.control.scroll.text.SystemText;
	import newx.com.loader.SwfLoader;
	
	public class ChatUI extends BaseUI
	{
		private static var _self	:ChatUI;
		
		private var _model				:ModelLocator;
		private var _chatLogic			:ChatLogic;
		private var _bindManager		:BindManage;
		
		public var inputArea			:MovieClip;
		public var systemArea			:MovieClip;
		public var outputArea			:MovieClip;
		public var channelList			:NxList;
		public var channelListBG		:MovieClip;
		public var systemLog			:MovieClip;
		
		private var _bindIndex			:int		=	-1;
		private var _isCastleMaster	:Boolean	=	false;
		
		private var _chanelBtn			:SimpleButton;
		private var _sendBtn			:SimpleButton;
		private var _picBtn			:SimpleButton;
		private var _chanelTxt			:TextField;
		public var _inputTxt			:TextField;
		
		//输出面板
		private var _outputPane		:Sprite;
		private var _clearBtn			:SimpleButton;
		private var _lockBtn			:SimpleButton;
		private var _unLockBtn			:SimpleButton;
		private var _bottomBtn			:SimpleButton;
		private var _zoomOutBtn		:SimpleButton;
		private var _zoomInBtn			:SimpleButton;
		
		private var _channelGroup		:ChatChannelGroup;
		
		private var _menuArr			:Array	=	[];
		
		private var _maxOutputLength	:int	=	100;
		
		private var _currentDisplay	:int	=	1;
		
		//数据记录
		private var _isHide			:Boolean	=	false;
		private var _isSetCastle		:Boolean	=	false;
		
		//缩放聊天区域
		private var _currentSize		:int	=	2;
		private var _outputPointList	:Array	=	[];
		private var _outputSizeList	:Array	=	[];
		private var _totalSize			:int	=	2;
		
		//自己的聊天记录
		private var _myChatMsgList		:Array	=	[];
		private var _myChatMsgIndex	:int	=	0;
		private var _first				:Boolean;
		private var _viewBox			:FaceIconViewUI;
		private var _chatText			:ChatText;
		private var _systemText		:SystemText;
		
		/**
		 *界面偏移，调了界面后界面全为(0,0),ChatUI特殊处理 
		 */		
		private const WIDTHRIGHT		:Number	=	22;
		private const UIOFFSET			:Number	=	452;
		private const UIOFFSETX		:Number	=	10;
		private const UIOFFSETWIDTH	:Number	= 	7;
		private const UIOFFSETHIGHT	:Number	= 	27;
		
		
		private var _load:URLLoader;
		
		private var _request:URLRequest;
		public var mc001:MovieClip;
		
		private var _bindVip : int = -1;
		public static function getInstance():ChatUI
		{
			if(_self == null)
			{
				_self	=	new ChatUI();
				
			}
			return _self;
		}
		
		public function ChatUI(popupUI:Boolean=false)
		{
			super(popupUI);
		}
		
		override public function Init():void
		{
			
			_request	=	new URLRequest(SwfLoader.CDN+"res/attack.xml");
			_load		=	new URLLoader();
			
			_chatLogic	=	ChatLogic.getInstance();
			_model	=	ModelLocator.getInstance();
			_bindManager	=	BindManage.getInstance();
			_viewBox		=	FaceIconViewUI.getInstance();
			UIManage.getInstance().initUI( _viewBox, "FaceIconViewUI" );
			_viewBox.callBackFun	=	viewBoxFunction;	
//			outputArea.width = inputArea.width	+	7;
			outputArea.x		=	systemArea.x	=	systemLog.x	=	0;
			//初始化缩放信息
			_outputPointList.push(new Point(outputArea.x,0));
			_outputPointList.push(new Point(outputArea.x,outputArea.y));
			_outputPointList.push(new Point(outputArea.x,outputArea.y-outputArea.height));
//			_outputPointList.push(new Point(outputArea.x,outputArea.y-outputArea.height*2));
			
			_outputSizeList.push({width:inputArea.width -2,height:0});
			_outputSizeList.push({width:inputArea.width -2,height:outputArea.height});
			_outputSizeList.push({width:inputArea.width- 2,height:outputArea.height*2});
//			_outputSizeList.push({width:inputArea.width- 2,height:outputArea.height*3});
			
			//输入面板
			_chanelBtn		=	inputArea["btn1"] as SimpleButton;
			_sendBtn		=	inputArea["btn2"] as SimpleButton;
			_picBtn			=	inputArea["btn3"] as SimpleButton;
			_viewBox.showBtn=	_picBtn;
			_chanelTxt		=	inputArea["txt1"] as TextField;
			_inputTxt		=	inputArea["txt2"] as TextField;
			_inputTxt.maxChars = 100;
//			_inputTxt.maxChars = 30;
			_inputTxt.restrict = "^{}";			
			inputArea.x += UIOFFSETWIDTH;
			_chanelBtn.enabled	=	false;
				
			//output bg 
			_chatText		= new ChatText();
			_chatText.x 	= outputArea.x + 20;
			_chatText.y 	= outputArea.y	+	7;
			_chatText.scrollBarPos =-9;
			_chatText.setSizeWH(outputArea.width - 23, outputArea.height - WIDTHRIGHT);
			addChild(_chatText);

			_systemText		= SystemText.getInstance();
			_systemText.x 	= systemArea.x + 20;
			_systemText.y 	= systemArea.y	+	7;
			_systemText.setSizeWH(systemArea.width - 23, 100);
			addChild(_systemText);
			
			_channelGroup	=	new ChatChannelGroup(_chatLogic.channelTypeData,"custSkin_0022",2);
			_channelGroup.y	=	outputArea.y	+ outputArea.height - _channelGroup.height
			_channelGroup.x	=	40;
			addChild(_channelGroup);
			_chatLogic.channelGroup = _channelGroup;
			
			_outputPane = new Sprite();
			_lockBtn	=	inputArea["btn8"];//锁
//			_lockBtn.x = 0;
//			_lockBtn.y = 0;			
//			_outputPane.addChild(_lockBtn);
			_unLockBtn	=	inputArea["btn10"];//锁
//			_unLockBtn.visible	=	false;
//			_unLockBtn.x = 0;
//			_unLockBtn.y = 0;
//			_outputPane.addChild(_unLockBtn);
			_clearBtn	=	inputArea["btn9"];
//			_clearBtn.x = 20;
//			_clearBtn.y = 0;
//			_outputPane.addChild(_clearBtn);
			_zoomOutBtn	=	inputArea["btn6"];//缩小
//			_zoomOutBtn.x = 40;
//			_zoomOutBtn.y = 0;
//			_outputPane.addChild(_zoomOutBtn);
			_zoomInBtn	=	inputArea["btn5"];//放大
//			_zoomInBtn.x = 60;
//			_zoomInBtn.y = 0;
//			_outputPane.addChild(_zoomInBtn);
			_bottomBtn	=	inputArea["btn7"];
//			_bottomBtn.x = 80;
//			_bottomBtn.y = 0;
//			_outputPane.addChild(_bottomBtn);
			_outputPane.x = _chatText.x + (_chatText.showWidth - _outputPane.width);
			_outputPane.y = _chatText.y - 18;
			addChild(_outputPane);
			
			//初始化输入面板
			_chanelTxt.text	=	_chatLogic.currentChannelStr;
			_inputTxt.text	=	"";
			
			channelListBG = ResManager.newResClass( "CustomControl_088" ) as MovieClip;
			addChild( channelListBG );
			channelListBG.mouseChildren = channelListBG.mouseEnabled =false;
			
			//频道列表
			channelList	=	new NxList();
			channelList.width=40;
			channelList.itemSkin = "Button_38";
			channelList.vScrollSkin = "vScrol_1";
			channelList.hScrollSkin = "vScrol_2";
			channelList.dataProvider=[];
			channelList.itemAlign="center";
			
			channelList.x	=	0;
			addChild(channelList);
			
			channelListBG.x = channelList.x;
			
			channelListBG.width = channelList.width;
			
			channelList.visible	=	channelListBG.visible = false;
			
			//菜单数据
//			_menuArr.push(StaticIni.getSwfLangStr("Common_PK"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Whisper"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Team"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Friend"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Watch"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Exchange"));
			_menuArr.push(StaticIni.getSwfLangStr("Common_Black"));
			
			outputArea.alpha = systemArea.alpha = systemLog.alpha	= 0;
			initEvent();
			updateOutPut();
			
			_model.addEventListener( SceneEvent.SceneLoaded, onSceneLoaded );
		}
		
		private function onSceneLoaded( evt : Event ):void
		{
			_model.removeEventListener( SceneEvent.SceneLoaded, onSceneLoaded );
			
			var vipLevel : int = _model.scene.Role.getVipLevel();
			//频道列表
			var count:int=0;
			for(var i:int=0;i<_chatLogic.channelData.length;i++)
			{
				var tmpChannel:int	=	_chatLogic.channelData[i];
				if(_chatLogic.channelStrList[tmpChannel]!=null)
				{
					if(tmpChannel!=GameDefine.CHATTYPE_CASTLE_MASTER)
					{
						if( tmpChannel == GameDefine.CHATTYPE_WORLD)continue;
						channelList.addItem({label:_chatLogic.channelStrList[tmpChannel],data:tmpChannel});//height : 22
						count++;
					}
				}
			}
			channelList.showCount=count;
			
//			if( vipLevel < 4 )
//			{
//				channelListBG.height = channelList.height + 2;
//				channelList.y		=	120 - channelList.height+UIOFFSET+6;
//				_bindVip = BindManage.getInstance().BindProp( _model.scene.Role, "VIPLevel",this,"viplevel");
//			}else
//			{
				channelListBG.height = channelList.height + 8;
				channelList.y		=	120 - channelList.height+UIOFFSET;
//			}
			channelListBG.y		=	channelList.y -2;
			channelList.currentSelect	=	0;
		}
		
		private function initEvent():void
		{
			_inputTxt.addEventListener(MouseEvent.CLICK,	onClickInput);
			_chatLogic.addEventListener(ChatEvent.CHAT_CHANEL_CHANGE, changeChanel);
			
			_chatLogic.addEventListener(ChatEvent.CHAT_GETMSG, getMsg);
			
			_chatLogic.addEventListener(ChatEvent.CHAT_ITEMS_CLICK,getChangeMsg);
			
//			_chanelBtn.addEventListener(MouseEvent.MOUSE_UP, showChanel);
			
			channelList.addEventListener(NxListEvent.LIST_CLICK, hideChanel);
			
			_sendBtn.addEventListener(MouseEvent.MOUSE_DOWN, send);
			
			_picBtn.addEventListener(MouseEvent.MOUSE_DOWN, picBox);
			
			StageInfo.stage.addEventListener(KeyboardEvent.KEY_DOWN,onEnter);
			
			_clearBtn.addEventListener(MouseEvent.CLICK,clearMsg);
			
			_clearBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			
			_clearBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_lockBtn.addEventListener(MouseEvent.CLICK,lockChat);
			_lockBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			_lockBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_unLockBtn.addEventListener(MouseEvent.CLICK,lockChat);
			_unLockBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			_unLockBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_bottomBtn.addEventListener(MouseEvent.CLICK,onBottom);
			_bottomBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			_bottomBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_zoomOutBtn.addEventListener(MouseEvent.CLICK,onZoomOut);
			_zoomOutBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			_zoomOutBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_zoomInBtn.addEventListener(MouseEvent.CLICK,onZoomIn);
			_zoomInBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			_zoomInBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
			
			_chatText.addLinkClickHandel(showMenuList);
			_systemText.addLinkClickHandel(showMenuList);
			_inputTxt.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		
			this.addEventListener(MouseEvent.ROLL_OUT,hideBG);
			this.addEventListener(MouseEvent.ROLL_OVER,showBG);
			
			outputArea.addEventListener(MouseEvent.MOUSE_DOWN,clickChatToSceneDown);
			outputArea.addEventListener(MouseEvent.MOUSE_UP,clickChatToSceneUp);
			
			systemArea.addEventListener(MouseEvent.MOUSE_DOWN,clickChatToSceneDown);
			systemArea.addEventListener(MouseEvent.MOUSE_UP,clickChatToSceneUp);
		}
		private function onClickInput(e:MouseEvent):void
		{
			WhisperLogic.enterobj =_inputTxt;
		}
		private function showTips(e:MouseEvent):void
		{
			var msgStr:String	=	"";
			switch(e.currentTarget)
			{
				case _clearBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1000376");
					break;
				case _lockBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1001116");
					break;
				case _unLockBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1001117");
					break;
				case _bottomBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1000377");
					break;
				case _zoomOutBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1000379");
					break;
				case _zoomInBtn:
					msgStr	=	StaticIni.getSwfLangStr("UIStr_1000380");
					break;
			}
			
			TipsMainLogic.instance.showString( msgStr );
		}
		
		private function hideTips(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		private function clickChatToSceneDown(e:MouseEvent):void
		{
			SceneManager.getInstance().norSceneView.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}
		
		private function clickChatToSceneUp(e:MouseEvent):void
		{
			SceneManager.getInstance().norSceneView.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
		}
		
		private function showBG(e:MouseEvent):void
		{
			outputArea.alpha = systemArea.alpha = systemLog.alpha	= 1;
			
			if(_currentSize == 0)
			{
				_chatText.hideScrollBar();
				_systemText.hideScrollBar();
			} else
			{
				_chatText.showScrollBar();
				_systemText.showScrollBar();
			}
			
		}
		
		private function hideBG(e:MouseEvent):void
		{
			outputArea.alpha = systemArea.alpha = systemLog.alpha	= 0;
			
			_chatText.hideScrollBar();
			_systemText.hideScrollBar();
			
			TipsMainLogic.instance.hide();
		}
		
		/**
		 * 缩小区域 
		 * @param e
		 * 
		 */
		private function onZoomOut(e:MouseEvent):void
		{
			if(_currentSize==0)
			{
				return;
			}
			_currentSize--;
			updateOutPut();
		}
		
		/**
		 * 放大区域 
		 * @param e
		 * 
		 */
		private function onZoomIn(e:MouseEvent):void
		{
			if(_currentSize == _totalSize)
			{
				return;
			}
			_currentSize++;
			updateOutPut();
		}
		
		private function updateOutPut():void
		{
			systemArea.visible = systemLog.visible = true;
			
			outputArea.x	= _outputPointList[_currentSize].x;
			outputArea.y	= _outputPointList[_currentSize].y;
			outputArea.height	= _outputSizeList[_currentSize].height;
			
			systemArea.x	= outputArea.x;
			systemArea.y	= outputArea.y - systemArea.height;
			
			systemLog.x			= outputArea.x;
			systemLog.y			= systemArea.y - systemLog.height;
			
			if(outputArea.height == 0)
			{
				_chatText.scrollBarPos =-14;
				_chatText.setSizeWH(_outputSizeList[_currentSize].width - 30 , 0);
				_systemText.setSizeWH(_outputSizeList[_currentSize].width - 30 , 80);
				outputArea.alpha = 0;
				_chatText.hideScrollBar();
				TipsMainLogic.instance.hide();
				
				systemArea.y	=  _outputPointList[1].y - 20;
				systemLog.y		= systemArea.y - systemLog.height;
				_chatText.y		=	outputArea.y +	5*(_currentSize)	+	3;
				_systemText.y	=	systemArea.y;
			}
			else
			{
				_chatText.scrollBarPos =-14;
				_chatText.setSizeWH(_outputSizeList[_currentSize].width - 30 , _outputSizeList[_currentSize].height  - WIDTHRIGHT-5*_currentSize-3 );
				_systemText.setSizeWH(_outputSizeList[_currentSize].width - 30 , 80 );
				outputArea.alpha = systemArea.alpha = systemLog.alpha	=	1;
				_chatText.showScrollBar();
				_systemText.showScrollBar();
				_chatText.y		=	outputArea.y +	5*(_currentSize)	+	3;
				_systemText.y	=	_chatText.y - systemArea.height;
			}
						
			_chatText.addLinkClickHandel(showMenuList);
			_systemText.addLinkClickHandel(showMenuList);
			_outputPane.x = _chatText.x + (_chatText.showWidth - _outputPane.width);
			_outputPane.y = _chatText.y - 18;
		}
		
		private function onBottom(e:MouseEvent):void
		{
			_chatText.scrollV	=	_chatText.MaxScrollV;
			_systemText.scrollV	=	_systemText.MaxScrollV;
		}
		
		private function lockChat(e : MouseEvent):void
		{
			if(e.target	==	_unLockBtn)
			{
				_unLockBtn.visible	=	false;
				_lockBtn.visible	=	true;
				_chatText.isLock	=	false;
				_systemText.isLock	=	false;
			}
			else
			{
				_unLockBtn.visible	=	true;
				_lockBtn.visible	=	false;
				_chatText.isLock	=	true;
				_systemText.isLock	=	true;
			}
		}
		
		private function showMenuList(val:String):void
		{
			if( val.indexOf("item_") != -1 )
			{
				//点击的是装备,道具的时候
				onShowItem(val);
				return;
			}else if( val.indexOf( "notice_") != -1 )
			{//显示tip
				var config : String = val.substr( 7, val.length );
				var item : Object = TipsMainLogic.instance.getStaticObjWithID(config)//StaticIni.getIniObj( config ) ;
				
				TipsMainLogic.instance.showMain( item );
				
				return;
			}else if(val.indexOf("Family_") != -1)
			{
				SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_GUILD_REQUEST_JOIN_GUILD, new WString(  val.substr( 7, val.length ) ) );
				return;
			}
			//点击的是玩家的时候
			if(_model.scene.Role.getPlayerName() ==val) return;
			MenuListManager.clickPlayer(val);
			_model.dispatchEvent(new SceneEvent(SceneEvent.PlayerClick,[val]));
		}
		
		/**
		 * 	显示道具tips
		 */
		private function onShowItem(tips : String):void
		{
			var tipsList : Array = tips.split(",");
			var configID : int = tipsList[1];
			var bindType : int = tipsList[2];
			var bindStatus : int = tipsList[3];
			
			//特殊属性
			var addProps : Array = tipsList[4].split(";");
			var holeCount : int = tipsList[5]; 
			var intensityLevel:int	=	tipsList[6];
			var intensityType:int	=	tipsList[7];
			var fightPower:int=	tipsList[8];
			var sellPrice:int=	tipsList[9];
			
			//和谐			
			var tObj:ObjectData = ObjectData.fromObj(StaticIni.getIniObj(String(configID)));
			tObj.HandleProperties("ConfigID",configID);
			
			if(tObj != null)
			{				
				tObj.HandleProperties("ConfigID", configID);
				tObj.HandleProperties("BindType", bindType);
				tObj.HandleProperties("BindStatus", bindStatus);
				tObj.HandleProperties("HoleCount", holeCount);
				tObj.HandleProperties("IntensityLevel",intensityLevel);
				tObj.HandleProperties("IntensityType",intensityType);
				//装备战斗力
				tObj.HandleProperties("ItemFightPower",fightPower);
				//售价
				tObj.HandleProperties("SellValue",sellPrice);
				var len:int = parseInt(StaticIni.getIniVar("ProTipList","addPropNum"));
				for(var l:int=0; l < len; ++l)
				{
					if(addProps[l] != null)
					{
						tObj.HandleProperties("AddPropNo"+l, addProps[l]);
					}
					else
					{
						tObj.HandleProperties("AddPropNo"+l, 0);
					}
				}
//				if(tObj.CheckProperties("EquipType") && tObj.GetProperties("EquipType") != "")
//				{
					TipsMainLogic.instance.showMain( tObj );
//				}
//				else
//				{
//					TipsMainLogic.instance.show
//					TipsManage.ShowItemTips(configID,bindType,bindStatus);
//				}
			}
		}
		private function getChatContents():Array
		{
			var chatContents : Array = [];
			var chatList : Array = _chatLogic.nxChatData;
			var leng : int = chatList.length;
			if (leng == 0) 
			{
				return [];
			}
			for( var i : int = leng - 1; i >= 0; i-- )
			{
//				if( _channelGroup.channelTypeArr.indexOf(chatList[i][0]) != -1 
//					|| chatList[i][0] == GameDefine.CHATTYPE_TOGM || chatList[i][0] == GameDefine.CHATTYPE_GM_HELP)
//				{
//					chatContents.unshift( chatList[i][1] );
//				}
				
				if( _channelGroup.channelTypeArr.indexOf(chatList[i][0]) != -1 &&
					( chatList[i][0] == GameDefine.CHATTYPE_WORLD_BY_MONEY ||
					  chatList[i][0] == GameDefine.CHATTYPE_GUILD ))
				{
					chatContents.unshift( chatList[i][1] );
				}
				
//				if( _channelGroup.channelTypeArr.indexOf(chatList[i][0]) == -1 && chatList[i][0] == GameDefine.CHATTYPE_SYSTEM 
//					&& chatList[i].length == 2 )
//				{//当时系统消息，并且需要屏蔽的时候，显示必须显示的系统消息
//					chatContents.unshift( chatList[i][1] );
//				}
				if( chatContents.length > _maxOutputLength )
				{
					break;
				}
			}
			
			return chatContents;
		}
		/**
		 * 返回单句聊天内容
		 * @return
		 */
		private function getChatContentStr():Array
		{
			var chatContentStr : Array = [];
			var chatList : Array = _chatLogic.nxChatData;
			var leng : int = chatList.length -1;
			if (leng < 0) 
			{
				return null
			}
			if( _channelGroup.channelTypeArr.indexOf(chatList[leng][0]) != -1 
				|| chatList[leng][0] == GameDefine.CHATTYPE_TOGM || chatList[leng][0] == GameDefine.CHATTYPE_GM_HELP)
			{
				chatContentStr 	=	chatList[leng]
			}
			
			return chatContentStr;
		}
		/**
		 * 频道聊天消息，全改变
		 * @param	e
		 */
		private function getChangeMsg(e:ChatEvent=null):void 
		{
			var chatList : Array = getChatContents();
//			if (chatList == null) return;
			var leng : int = chatList.length;
			var disStr:String	=	"";
			_chatText.clearTalkText();
			if (_chatText.isLock)
			{
				_unLockBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
			for( var i : int = 0; i < leng; i++ )
			{
				_chatText.insterTalkStr(chatList[i]);
			}
			
			var len:int	=	channelList.dataProvider.length;
			for(var j:int=0;j<len;j++)
			{
				var obj:Object	=	channelList.getItemByIndex(j);
				if(_channelGroup.clickedBtn && _channelGroup.clickedBtn.label == obj.label)
				{
					channelList.currentSelect	=	j;
				}
			}
		}
		/**
		 * 获得服务器返回聊天消息 
		 * @param e
		 * 
		 */
		private function getMsg(e:ChatEvent=null):void
		{
			var chatList : Array = e._data as Array;//getChatContentStr();
			if (chatList != null ) 
			{
				if(chatList[0] == GameDefine.CHATTYPE_WORLD_BY_MONEY || chatList[0] == GameDefine.CHATTYPE_GUILD)
				{
					_chatText.insterTalkStr(chatList[1]);
				}
				else
				{
					_systemText.insterTalkStr(chatList[1]);
				}
			}
		}
		
		private function showChanel(e:MouseEvent):void
		{
			if(channelList.visible == true)
			{
				channelList.visible	=	false;
				channelListBG.visible = false;
			}
			else
			{
				channelList.visible	=	true;
				channelListBG.visible = true;
				if(channelList.parent)
				{
					channelList.parent.setChildIndex(channelList,channelList.parent.numChildren-1);
				}
			}
		}
		
		private function hideChanel(e:NxListEvent):void
		{
//			var tmpItem:NxListItem	=	e.item;
			channelList.visible	=	false;
			channelListBG.visible = false;
			_chatLogic.currentChannel	=	int(e.itemData.data);
			
//			var arr:Array	=	_channelGroup.btnArr;
//			for(var i:int=0;i<arr.length;i++)
//			{
//				var tmpBtn:ChatButton	=	arr[i];
//				
//			}
//			getChangeMsg();
		}
		
		/**
		 * 快捷键
		 * 回车显示输入框，有文字发送，没文字关闭输入 
		 * @param e
		 * 
		 */
		private function onEnter(e:KeyboardEvent):void
		{
			if(e.keyCode	==	Keyboard.ENTER)
			{
				if(WhisperLogic.enterobj ==null || WhisperLogic.enterobj != _inputTxt) return;
				if(_inputTxt.text!="")
				{
					sendMsg(true);
				}
				
				StageInfo.stage.focus	=	_inputTxt;
				WhisperLogic.enterobj =_inputTxt;
			}
		}
		
		private function changeChanel(e:ChatEvent):void
		{
			WhisperLogic.enterobj =_inputTxt;
			if(_chatLogic.currentChannelStr!=null)
			{
				_chanelTxt.text	=	_chatLogic.currentChannelStr;
				StageInfo.stage.focus	=	_inputTxt;
				
			}
		}
		
		private function send(e:MouseEvent) : void
		{
			if(_inputTxt.text!="")
			{
				if(_inputTxt.text == "/LookEffect")
				{
					_load.load(_request);
					_load.addEventListener(Event.COMPLETE,completeHandle);
				}
				else
				{
					sendMsg(true);
				}
				
			}
		}
		/**
		 * 看战报 
		 * @param e
		 * 
		 */		
		private function completeHandle(e:Event):void
		{
			var str:String	=	String(e.currentTarget.data);
			AttackLogic.getInstance().setAttackXML(str);
		}
		private function picBox(e:MouseEvent):void
		{
			if(_viewBox.created)
			{
				_viewBox.removeStageEvent();
				_viewBox.hide();

			}
			else
			{
				_viewBox.show(ChatUI.getInstance());
				_viewBox.addStageEvent();
				_viewBox.x	=_picBtn.x;
				_viewBox.y	=	390;
			}
		}
		
		/**
		 *	保存聊天记录 
		 * @param message		聊天信息
		 * 
		 */
		private function saveChatMsg(message : String):void
		{
			//判断是否有重复
			for(var i : int = 0; i < _myChatMsgList.length; i++)
			{
				if(_myChatMsgList[i] == message)
				{
					_myChatMsgList.splice(i, 0);
					break;
				}
			}
			
			_myChatMsgList.push(message);
			
			if(_myChatMsgList.length > 10)
			{//大于10跳，把最先的删除
				_myChatMsgList.splice(1, 0);
			}
			
			_myChatMsgIndex	=	_myChatMsgList.length - 1;
			
			_first = true;
		}
		
		/**
		 * 发送聊天信息 
		 * 
		 * @param isHtml   是否解析html
		 * 
		 */
		private function sendMsg(isHtml: Boolean):void
		{
			var message : String = _inputTxt.text;
			
//			TrumpetUI.generalMsg(message);
			
			/*if( message == "killFight" )
			{
				AttackLogic.getInstance().fightLogic.killFight();
			}*/
		
			if(isHtml)
			{//保存聊天的记录
				saveChatMsg(message);
			}
			
			var id : String = canSendMsg();
			if(id !="" && message.indexOf("/") != 0)
			{
				//模拟聊天频道
				MessageUI.getInstance().showInfo(StaticIni.getSwfLangStr(id));
				_chatLogic.setChatData({msg:StaticIni.getSwfLangStr(id), strID:id, type:GameDefine.CHATTYPE_SYSTEM});
				_inputTxt.text	=	"";
				return;
			}
			
			if(isHtml)
			{
				message = htmlCode(message);
			}
			
			if(message.indexOf("/") != 0)
			{
				_inputTxt.text	=	"";
			}else
			{
				_inputTxt.text	=	message.substr(0, message.indexOf(" ") + 1);
			}
			
			
			//gm求助的时候
			if(isGMCC())
			{
				if(message.indexOf("/") == 0)
				{
					MessageUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIStr_1001085"));
					_chatLogic.setChatData({msg:StaticIni.getSwfLangStr("UIStr_1001085"), strID:"UIStr_1001085", type:GameDefine.CHATTYPE_SYSTEM});
					return;
				}
				//模拟聊天频道
				_model.dispatchEvent(new CustomMsgEvent(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT),[GameDefine.CHATTYPE_TOGM, ChatFilterLogic.getInstance().filter(message)]));
				SystemSender.getInstance().SendGMCCTOServer(message);
				return;
			}
			if(_chatLogic.currentChannel == GameDefine.CHATTYPE_WORLD || (_chatLogic.currentChannel == GameDefine.CHATTYPE_WORLD_BY_MONEY || _chatLogic.currentChannel == GameDefine.CHATTYPE_All) && message.indexOf("/") != 0)
			{
				var mesComp:int	=	_model.scene.Role.GetPropertiesEx("Camp");	
				message	+=	mesComp;
			}
			
			_chatLogic.dispatchEvent(new ChatEvent(ChatEvent.CHAT_SENDMSG,message));
		} 
		
		private function canSendMsg():String
		{
			var channel : int = _chatLogic.currentChannel;
			
			var tips : String = "";
			
			if(channel == GameDefine.CHATTYPE_GUILD && _model.scene.Role.GetPropertiesEx("GuildName") == "")
			{//家族聊天&7没有家族
				return "UIStr_1001038";
			}
			
//			if(channel == GameDefine.CHATTYPE_TEAM && !_model.scene.GetRecordEx("team").recArr.length>0)
//			{//队伍频道&&没有队伍
//				return "UIStr_1001039";
//			}
			
			if(channel == GameDefine.CHATTYPE_CAMP && _model.scene.Role.GetProperties("Level") < 10)
			{//阵营聊天限制等级10级
				return "UIStr_1001638";
			}
			
			return "";
		}
		
		public function htmlCode(content : String) : String
		{
			var msg : String = content;
			var myPattern:RegExp = new RegExp("<","g");  
			var myPattern1:RegExp = new RegExp(">","g");  
			
			msg = msg.replace(myPattern,"&lt;");
			msg = msg.replace(myPattern1,"&gt;");
			return msg;
		}
		
		/**
		 * 清除聊天信息 
		 * @param e
		 * 
		 */
		private function clearMsg(e:MouseEvent):void
		{
			_chatLogic.clearData();
			_chatText.clearTalkText();
			_systemText.clearTalkText();
			if (_chatText.isLock)
			{
				_unLockBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		public  function viewBoxFunction(...argc):void
		{
			if(_inputTxt.text.length > _inputTxt.maxChars - argc[0].length)
			{
				return;
			}
			_inputTxt.replaceText(_inputTxt.caretIndex ,_inputTxt.caretIndex ,argc[0]);
			
		}
		public function setWisper(str:String):void
		{
			StageInfo.stage.focus	=	_inputTxt;
			_inputTxt.text	=	"/"+str+" ";
			_inputTxt.setSelection(_inputTxt.text.length,_inputTxt.text.length);
		}
		
		public function showItem(tObj:ObjectData,parentViewID:int):void
		{
			//GM频道不能装备展示
			if(isGMCC())
			{
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("UIStr_1000911"));
				return;
			}
			
			_inputTxt.text = htmlCode(_inputTxt.text);
			
			var configID:String = tObj.GetPropertiesEx("ConfigID");
			var bindStatus:int = tObj.GetPropertiesEx("BindStatus");
			var bindType:int = tObj.GetPropertiesEx("BindType");
			var holeCount:int = tObj.GetPropertiesEx("HoleCount");
			var intensityLevel:int=tObj.GetPropertiesEx("IntensityLevel");
			var intensityType:int	=	tObj.GetPropertiesEx("IntensityType");
			var fightPower:int	=	tObj.GetPropertiesEx("ItemFightPower");
			var sellPrice:int	=	tObj.GetPropertiesEx("SellValue");
			//附加属性
			var addProps : String = "";
			var len		:int	=	int(StaticIni.getIniVar("ProTipList","addPropNum"));
			for(var i : int = 0; i < len; i++){
				addProps += tObj.GetPropertiesEx("AddPropNo"+i) + ";";
			}
			
			//装备名
			var itemName :String	= StaticIni.getIniVar(configID,"NameID");
			
			var color :int = int(StaticIni.getIniVar(configID,"Color"));
			itemName = StaticIni.getSwfLangStr(itemName);
			itemName = link_color(itemName,color);
			
			var myName:String = _model.scene.Role.GetPropertiesEx("Name");
			
			var itemUrl:String = "<a href=\"event:item_"+ "," +configID + "," + bindType + "," 
				+ bindStatus + "," + addProps  +","+holeCount+","+intensityLevel+ ","+intensityType+","+fightPower+","+sellPrice+"\">"
				+ StaticIni.getSwfLangStrVar("UIStr_1000587",[itemName])+"</a>";
			
			_inputTxt.appendText(itemUrl);
			
			
			sendMsg(false);
		}
		
		private function link_color(str:String,color:int):String
		{
			if(color < 1 || color > 6)
			{
				color	=	1;
			}
			return ("<font color ='"+ItemMaxTips.COLOR_TABLE[color]+str+"</font>");
		}

		public function get isHide():Boolean
		{
			return _isHide;
		}
		/**
		 *	是否是gmcc频道 
		 * @return 
		 * 
		 */
		private function isGMCC() : Boolean
		{
			return _chatLogic.currentChannel == GameDefine.CHATTYPE_GM_HELP;
		}
		
		private function onKeyUp(evt : KeyboardEvent):void
		{
			WhisperLogic.enterobj =_inputTxt;
			if(_myChatMsgList.length == 0) return;
			if(evt.keyCode != Keyboard.UP && evt.keyCode != Keyboard.DOWN)
			{
				return;
			}
			
			//第一次显示上次的内容
			if(_first)
			{
				_inputTxt.text = _myChatMsgList[_myChatMsgIndex];
				_first	=	false;
			}else
			{
				if(evt.keyCode	==	Keyboard.UP)
				{//按上按钮
					_myChatMsgIndex--;
					if(_myChatMsgIndex < 0)
					{
						_myChatMsgIndex = 0;
					}
					_inputTxt.text = _myChatMsgList[_myChatMsgIndex];
				}else if(evt.keyCode == Keyboard.DOWN)
				{//按下按p钮
					_myChatMsgIndex++;
					
					if(_myChatMsgIndex >= _myChatMsgList.length)
					{
						_myChatMsgIndex	=	_myChatMsgList.length - 1;
					}
					_inputTxt.text = _myChatMsgList[_myChatMsgIndex];
				}
			}
			_inputTxt.setSelection(_inputTxt.text.length,_inputTxt.text.length);
		}
	}
}